package com.open.party.cloud.view

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import com.open.party.cloud.R
import com.open.party.cloud.view.examine.fragment.ExamineClassForMineListFragment
import com.open.party.cloud.view.fragments.YunKeTangListFragment
import com.open.party.cloud.view.home.article.fragment.ArticleListFragment
import com.open.party.cloud.viewModel.ArticleViewModel
import kotlinx.android.synthetic.main.ren_cai_main_fragment.*

class XueXiMainFragment : Fragment(){

    private var viewModel: ArticleViewModel? = null
    private var currView: View? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (currView == null) {
            currView = inflater.inflate(R.layout.ren_cai_main_fragment, container, false)
        }
        return currView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        viewModel = ArticleViewModel()
        initView()
    }

    private fun initView() {
        initTabView()
    }

    private fun initTabView() {
        val titleList = ArrayList<String>()
        titleList.add("凉都云课堂")
        titleList.add("培训云动态")
        titleList.add("培训考试云平台")

        val typeList = ArrayList<String>()
//        typeList.add("RCGZ_RCYJ")
//        typeList.add("HOME_ZGDT")
//        typeList.add("HOME_TZGG")

        initTab(
            titleList,
            getFragments(typeList)
        )
    }

    private fun getFragments(typeList: java.util.ArrayList<String>): List<Fragment> {
        val fragments: ArrayList<Fragment> = ArrayList()
        fragments.add(YunKeTangListFragment())
        // 人才
        val fragment = ArticleListFragment()
        val bundle = Bundle()
        bundle.putString("type", "DYGBJY_PXYDT")
        fragment.arguments = bundle
        fragments.add(fragment)
        //
        fragments.add(ExamineClassForMineListFragment())

//        ZhuanJiaFuWuListFragment()
//        for (type in typeList) {
//            val fragment = ArticleListFragment()
//            val bundle = Bundle()
//            bundle.putString("type", type)
//            fragment.arguments = bundle
//            fragments.add(fragment)
//
//            Log.e("type", type)
//        }

        return fragments
    }

    private fun initTab(titles: ArrayList<String>, fragments: List<Fragment>) {

        val fragmentPagerAdapter = object : FragmentPagerAdapter(childFragmentManager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            override fun getCount(): Int {
                return fragments.size
            }

            override fun getItem(arg0: Int): Fragment {
                return fragments[arg0]
            }
        }

        viewPager.offscreenPageLimit = titles.size - 1
        viewPager.adapter = fragmentPagerAdapter
        //        pager.addOnPageChangeListener(this);

        scrollTab.setTitles(titles, 0, titles.size <= 4)
//        scrollTab.setNumber(1, "9", View.VISIBLE)//设置数字红点
        scrollTab.setViewPager(viewPager)
        scrollTab.setOnTabListener { index, _ ->
            viewPager.setCurrentItem(index, true)
        }
    }

//    override fun onClick(position: Int, itemData: ArticleEntity, flag: String) {
//        XUtils.intent().openActivity(activity, ArticleDetailsActivity::class.java)
//            .putStringExtra("id", itemData.id)
//            .startInFragment(this)
//    }, OnSuperListener<ArticleEntity>
}